<?php

namespace backend\helpers;

use yii\helpers\ArrayHelper;
use yii\helpers\Html;

class TreeViewFormatterHelper
{

    public static function formatNode($node)
    {
        $childrenList = array();
        $result = array();

        $draftTag = 'Черновик';
        $hiddenTag = 'Скрыта';
        
        if($node->is_draft)
        {
            $result['text'] = '<span class="text-muted">' . $node->title . '<small> (ID: ' . $node->id . ' Depth: ' . $node->depth . ' Sort: ' . $node->sort . ')</small></span>';
        }
        else
        {
            $result['text'] = $node->title . '<small class="text-muted"> (ID: ' . $node->id . ' Depth: ' . $node->depth . ' Sort: ' . $node->sort . ')</small>';
        }
        

        $result['tags'] = [
            Html::a(
                '<i class="glyphicon glyphicon-pencil"></i>',
                ['update', 'id' => $node->id],
                ['title' => 'Редактировать']
            ),
            Html::a(
                '<i class="glyphicon glyphicon-eye-open"></i>',
                ['view', 'id' => $node->id],
                ['title' => 'Посмотреть']
            ),
            Html::a(
                '<i class="glyphicon glyphicon-arrow-down"></i>',
                ['move-down', 'id' => $node['id']],
                ['title' => 'Передвинуть вниз']
            ),
            Html::a(
                '<i class="glyphicon glyphicon-arrow-up"></i>',
                ['move-up', 'id' => $node['id']],
                ['title' => 'Передвинуть вверх']
            ),
        ];

        if($node->is_draft)
        {
            array_push($result['tags'], $draftTag);
        }

        if($node->is_hidden)
        {
            array_push($result['tags'], $hiddenTag);
        }
        
        if(!empty($node->children))
        {
            foreach ($node->children as $children) {
                array_push($childrenList, self::formatNode($children));
            }
            $result['nodes'] = $childrenList;
        }
        
       // print_r($result);
        //die;

        return $result;
    }

}